package generator 
{
	import flash.geom.Vector3D;
	import nl.jorisdormans.phantom2D.core.Component;
	import nl.jorisdormans.phantom2D.core.Composite;
	import nl.jorisdormans.phantom2D.objects.GameObject;
	import nl.jorisdormans.phantom2D.objects.Tile;
	import nl.jorisdormans.phantom2D.objects.TiledObjectLayer;
	/**
	 * ...
	 * @author Joris Dormans
	 */
	public class Generator extends Component
	{
		protected var tiledObjectLayer:TiledObjectLayer;
		public var drawTile:int = 1;
		
		public function Generator() 
		{
		}
		
		override public function onAdd(composite:Composite):void 
		{
			super.onAdd(composite);
			tiledObjectLayer = getParentByType(TiledObjectLayer) as TiledObjectLayer;
			if (!tiledObjectLayer) {
				throw new Error("TiledObjectLayer component must be added to a TiledObjectLayer or on of its components.");
			}
		}
		
		public function wrapTiles():void {
			
		}
		
		public function getCell(tile:Tile):Cell{
			for (var i:int = 0; i < tile.objects.length; i++) {
				var cell:Cell = tile.objects[i].getComponentByClass(Cell) as Cell;
				if (cell) return cell;
			}
			return null;
		}
		
		public function getCellAt(tileX:int, tileY:int):Cell {
			var index:int = tileX + tileY * tiledObjectLayer.tilesX;
			if (index<0 || index>=tiledObjectLayer.tiles.length) return null;
			var tile:Tile = tiledObjectLayer.tiles[index];
			for (var i:int = 0; i < tile.objects.length; i++) {
				var cell:Cell = tile.objects[i].getComponentByClass(Cell) as Cell;
				if (cell) return cell;
			}
			return null;
		}
		

		
		private function createCell(tile:Tile, type:int):void {
			var gameObject:GameObject = new GameObject();
			gameObject.addComponent(new Cell(type));
			tiledObjectLayer.addGameObjectSorted(gameObject, tile.getPosition());
			
		}
		
		public function clearTiles(type:int):void {
			trace("Clearing...");
			for (var i:int = 0; i < tiledObjectLayer.tiles.length; i++) {
				var tile:Tile = tiledObjectLayer.tiles[i];
				var cell:Cell = getCell(tile);
				if (cell) {
					cell.type = type;
					cell.life = 0;
				} else {
					createCell(tile, type);
				}
				
			}
		}
		
		public function generateRandomTiles(... types):void {
			trace("Randomizing tiles...", types);
			for (var i:int = 0; i < tiledObjectLayer.tiles.length; i++) {
				var r:int = Math.random() * types.length;
				var type:int = types[r];
				var tile:Tile = tiledObjectLayer.tiles[i];
				var cell:Cell = getCell(tile);
				if (cell) {
					cell.type = type;
					cell.life = 0;
				} else {
					createCell(tile, type);
				}
			}
		}
		
		public function setCellAt(x:int, y:int, type:int):void {
			var i:int = x + y * tiledObjectLayer.tilesX;
			if (i<0 || i>=tiledObjectLayer.tiles.length) return;
			var cell:Cell = getCell(tiledObjectLayer.tiles[i]);
			cell.type = type;
		}
		
		public function doIteration(callback:Function = null):void {
			if (callback != null) {
				for (var i:int = 0; i < tiledObjectLayer.tiles.length; i++) {
					callback(getCell(tiledObjectLayer.tiles[i]));
				}
			}
			
			for (i = 0; i < tiledObjectLayer.tiles.length; i++) {
				getCell(tiledObjectLayer.tiles[i]).applyNextType();
			}
		}
		
		public function iterate():void {
			//to be overriden
		}
		
		public function convertCellsToObjects():void {
			doIteration(generateObject);
		}
		
		private function generateObject(cell:Cell):void {
			cell.generateObject();
		}
		
		
	}

}